package com.lry.task;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

public class TopNTaskStrategy implements TaskStrategy{

    private int n;
    public TopNTaskStrategy(int n){
        this.n = n;
    }

    @Override
    public List<Integer> execute(List<Integer> list) {
        if(list.size()<=n){
            return list;
        }
        PriorityQueue<Integer> queue = new PriorityQueue<>(Comparator.reverseOrder());
        for (Integer val : list) {
            queue.add(val);
        }
        List<Integer> topN = new ArrayList<>();

        int copyN = n;
        while(copyN>0){
            topN.add(queue.poll());
            copyN--;
        }
        return topN;
    }
}
